﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace theguineasyndrome
{
    public class FireDrop : LiquidDrop
    {
        public const float LIFE_TIME = 3;
        public const int MAX_AGE = 350;

        private int duration;
        private int age;

        private SoundEffect cracklingSound;
        private Clouds clouds;

        public FireDrop(Level level, Clouds clouds, Texture2D texture, SoundEffect cracklingSound, Vector2 position, Vector2 speed, float lifeTime, int popTime, int duration)
            : base(level, texture, position, speed, lifeTime, popTime)
        {
            this.clouds = clouds;
            this.duration = duration;
            this.age = 0;
            this.cracklingSound = cracklingSound;

            color = Color.White;

            size = new Vector2(texture.Width, texture.Height);

            radius = (texture.Width + texture.Height) / 2 * 0.45f;

            rectangle.X = (int)position.X;
            rectangle.Y = (int)position.Y;
            rectangle.Width = (int)size.X;
            rectangle.Height = (int)size.Y;
            weight = 0.02f;
        }

        public int getAge()
        {
            return age;
        }

        public bool shouldBeDead()
        {
            return age > duration;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            age = (int)gameTime.TotalGameTime.TotalMilliseconds - popTime;

            float f = (float)age / MAX_AGE;

            if (f > 0.6f)
                f = 1 - (f - 0.6f) * 1f / 0.4f;

            f = 1 - (1 - f) * (1 - f);

            color.A = (byte)(255 * f);
            color.R = color.A;
            color.G = color.A;
            color.B = color.A;

            Enemy enemy = level.getCollidingEnemy(rectangle);
            if (enemy != null && enemy.canBurn())
            {
                clouds.add(enemy.Center);
                level.removeElement(enemy);
                cracklingSound.Play();
            }
        }

        public override void Draw(SpriteBatch spriteBatch, Camera camera)
        {
            spriteBatch.End();

            if (currentPortal != null)
            {
                spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, scissor);

                switch (currentPortal.getSide())
                {
                    case LevelElement.Side.LEFT:
                    {
                        int width = (int)(camera.getRelative(new Vector2(currentPortal.CenterX, 0)).X - camera.Left * 2);

                        if (width > 0 && width <= camera.Width)
                            spriteBatch.GraphicsDevice.ScissorRectangle = new Rectangle(0, 0, width, (int)camera.Height);

                        else
                            spriteBatch.GraphicsDevice.ScissorRectangle = voidRectangle;
                    }
                    break;

                    case LevelElement.Side.RIGHT:
                    {
                        int x = (int)(camera.getRelative(new Vector2(currentPortal.CenterX, 0)).X - camera.Left * 2);
                        int width = (int)(camera.Width - x);

                        if (width > 0 && width <= camera.Width)
                            spriteBatch.GraphicsDevice.ScissorRectangle = new Rectangle(x, 0, width, (int)camera.Height);

                        else
                            spriteBatch.GraphicsDevice.ScissorRectangle = voidRectangle;
                    }
                    break;

                    case LevelElement.Side.TOP:
                    {
                        int height = (int)(camera.getRelative(new Vector2(0, currentPortal.CenterY)).Y - camera.Top * 2);

                        if (height > 0 && height <= camera.Height)
                            spriteBatch.GraphicsDevice.ScissorRectangle = new Rectangle(0, 0, (int)camera.Width, height);

                        else
                            spriteBatch.GraphicsDevice.ScissorRectangle = voidRectangle;
                    }
                    break;

                    case LevelElement.Side.BOTTOM:
                    {
                        int y = (int)(camera.getRelative(new Vector2(0, currentPortal.getElement().Bottom)).Y - camera.Top * 2);
                        int height = (int)(camera.Height - y);

                        if (height > 0 && height <= camera.Height)
                            spriteBatch.GraphicsDevice.ScissorRectangle = new Rectangle(0, y, (int)camera.Width, height);

                        else
                            spriteBatch.GraphicsDevice.ScissorRectangle = voidRectangle;
                    }
                    break;
                }
            }
            else
                spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);

            float angle = getAngle();
            spriteBatch.Draw(texture, camera.getDstRect(rectangle), srcRect, color, angle, size / 2, SpriteEffects.None, 0);

            spriteBatch.End();
            spriteBatch.Begin();
        }
    }
}
